package top.lywivan.mm.controller;

import top.lywivan.mm.common.GlobalConst;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.entity.Result;
import top.lywivan.mm.pojo.Tag;
import top.lywivan.mm.pojo.User;
import top.lywivan.mm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @PostMapping("/findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = tagService.findListByPage(queryPageBean);
        return new Result(true,"分页查询成功",pageResult);
    }

    @PostMapping("/add")
    public Result addTag(@RequestBody Tag tag, HttpSession session){
        User user = (User) session.getAttribute(GlobalConst.SESSION_KEY_USER);
        if (user==null){
            user=new User();
            user.setId(1);
        }
        tag.setUserId(user.getId());
        tagService.addTag(tag);
        return new Result(true,"添加成功");
    }

    @GetMapping("/delete")
    public Result deleteTagById(Integer tagId){
        tagService.deleteTagById(tagId);
        return new Result(true,"删除成功");
    }

    @GetMapping("/findListByCourseId")
    public Result findListByCourseId(Integer courseId){
        List<Tag> list = tagService.findListByCourseId(courseId);
        return new Result(true,"根据学科获取标签成功",list);
    }
}
